"""
Простая игра "камень ножницы бумага".

Author: Milinuri Nirvalen
Ver: 1.0 (2)
"""

from chiori import Config
from chiori import Plugin

import random


p = Plugin(name='камень ножницы бумага', sp=['кнб', 'rps'])

names = {1:"камень", 2:"ножницы", 3:"бумага"}
base_config = {'wins':0, 'loses':0, 'drows':0}
path = "data/rps_stats.toml"


# main func
# =========

def process(event, arg):
	uid = str(event.get('from.id'))
	c = Config(uid, base_config, path)
	choise = random.randint(1, 3)
	text = f'[{names[arg]} / {names[choise]}] '

	if arg == choise:
		c.group_data['drows'] += 1
		text += "ничья!"

	elif arg == 1 and choise == 2 \
			or arg == 2 and choise == 3 or arg == 3 and choise == 1:
		c.group_data['wins'] += 1
		text += "победа!"

	else:
		c.group_data['loses'] += 1
		text += 'проигрыш!'

	c.save()
	return text


# commands
# ========

@p.command('камень')
async def rock(event, ctx):
	await ctx.message(process(event, 1))

@p.command('ножницы')
async def scissors(event, ctx):
	await ctx.message(process(event, 2))

@p.command('бумага')
async def paper(event, ctx):
	await ctx.message(process(event, 3))

@p.command('stat(s)', 'стат(а/ы)')
async def gameStats(event, ctx):
	c = Config(str(event.get('from.id')), base_config, path).group_data
	# ✂️🏆🎲
	await ctx.message(f'''🧹 статистика игр:
🎲 всего игр сыграно: {sum(c.values())}
🎯 {c["wins"]} побед (🏆 {round(c["wins"] / (sum(c.values())or 1) * 100, 2)}%)
🤝 {c["drows"]} ничьих
✂️ {c["loses"]} поражеий''')
